#include "WWBizUtility.h"
#include "WWBizUtilityInline.hpp"
#include "ParseHttpHead.hpp"
#include "../Core/msgbus_interface.h"
#include "../Core/threadpool.h"
#include "Base64Code.hpp"

using namespace NetMsgBus;
using namespace wwbiz;
int main()
{
    std::string instr("abcd1234&^%;'d/.dsh");
    CommonUtility::ToUpper(instr);
    assert(instr == "ABCD1234&^%;'D/.DSH");
    CommonUtility::ToUpper(instr, false);
    assert(instr == "abcd1234&^%;'d/.dsh");

    std::string ansistr_orig("12345我们在测试哦67890");
    std::wstring wcstr_orig(L"12345我们在测试哦67890");
    StrA2W wcstr_conv(ansistr_orig, "UTF-8");
    assert(wcstr_orig == std::wstring(wcstr_conv));
    StrW2A astr_conv(wcstr_orig, "UTF-8");
    assert(ansistr_orig == std::string(astr_conv));
    
    std::string result = Md5Hash::GetMD5("12345");
    char buf[33] = {'\0'};
    char tmp[3] = {'\0'};
    for(int i = 0; i< 16; i++)
    {
        sprintf(tmp, "%2.2x", (unsigned char)result[i]);
        strcat(buf, tmp);
    }
    assert(std::string(buf) == std::string("827ccb0eea8a706c4c34a16891f84e7b")); 
    assert(Md5Hash::ConvertMD5ToPrintable(result) == std::string("827ccb0eea8a706c4c34a16891f84e7b")); 

    result = Md5Hash::GetMD5FromFile("/Users/absolute/workspace/aliwwforlinux/3rd_tools/jsoncpp/config.h");
    assert(Md5Hash::ConvertMD5ToPrintable(result) == std::string("1ef69850c9ad62cbe613bf4890dd994b"));

    std::string trimtest("  123 4d5  ");
    std::string res1 = trimtest;
    CommonUtility::TrimStr(res1, 1);
    assert(res1 == "123 4d5  " );

    std::string res2 = trimtest;
    CommonUtility::TrimStr(res2, 2);
    assert(res2 == "  123 4d5" );

    std::string res3 = trimtest;
    CommonUtility::TrimStr(res3, 3);
    assert(res3 == "123 4d5" );

    std::wstring wtrimtest(L"  123 4d5  ");
    std::wstring wres1 = wtrimtest;
    CommonUtility::TrimStr(wres1, 1);
    assert(wres1 == L"123 4d5  " );

    std::wstring wres2 = wtrimtest;
    CommonUtility::TrimStr(wres2, 2);
    assert(wres2 == L"  123 4d5" );

    std::wstring wres3 = wtrimtest;
    CommonUtility::TrimStr(wres3, 3);
    assert(wres3 == L"123 4d5" );

    std::wstring httptest(L"HTTP/1.1 200 OK\r\nDate: Tue, 26 Jul 2011 09:52:26 GMT\r\nServer: Apache/2.2.9 (Unix)\r\nCache-Control: no-cache\r\nContent-Length: 300\r\nConnection: close\r\nContent-Type: text/html;charset=utf-8\r\n\r\n111.65.161.68:16000,114.71.161.123:443,160.75.101.14:80\r\n");
    std::string pteststr = StrW2A(httptest);
    CParseHttpHead parshttp(pteststr.data(), pteststr.size());
    if(!parshttp.Parse())
    {
        assert(0);
    }
    bool bmore;
    std::wstring body = parshttp.GetValueByKey(L"Content-Length", bmore);
    assert(body == std::wstring(L"300"));

    std::string destest("test");
    CDesEncryptMacCompatible des(0);
    des.SetKey("cntaobao");
    std::string desres = des.Encrypt(destest);
    assert(des.Decrypt(desres).c_str() == destest);

    std::string base64test_2("test");

    int len;
    CommonUtility::Base64Encode((unsigned char*)base64test_2.data(), (int)base64test_2.size(), NULL, len);
    std::string base64res;
    base64res.reserve(len);
    memset((char*)base64res.data(), 0, len);
    CommonUtility::Base64Encode((const uint8_t*)base64test_2.data(), (int)base64test_2.size(), (char*)base64res.data(), len);
    std::string base64decoderes;
    base64decoderes.reserve(len);
    CommonUtility::Base64Decode(base64res.data(), len, (uint8_t*)base64decoderes.data(), len);
    assert(base64decoderes.c_str() == base64test_2);

    //CDesEncrypt descode(0);
    //std::string _deskey2 = descode.SetKey("cntaobao");
    //std::string descoderes = descode.Encrypt(destest, _deskey2);
    //assert(descode.Decrypt(descoderes, _deskey2).c_str() == destest);

    std::string base64test("test");
    std::string baseenstr;
    BizUtilityInline::EncodeXXX(base64test, baseenstr, L"cntaobao");
    std::string basedestr;
    BizUtilityInline::DecodeXXX(baseenstr, basedestr, L"cntaobao");
    assert(basedestr.c_str() == base64test);
    printf("test generate guid, %s.\n", CommonUtility::GenerateGuidStr().c_str());
    CDesEncryptMacCompatible desfiletest;
    desfiletest.SetKey("cntaobao");
    bool ret = desfiletest.EncryptFile("/Users/absolute/workspace/aliwwforlinux/Bin/readme.txt",
        "/Users/absolute/workspace/aliwwforlinux/Bin/readme.txt-encrypt");
    assert(ret);
    ret = desfiletest.DecryptFile("/Users/absolute/workspace/aliwwforlinux/Bin/readme.txt-encrypt",
        "/Users/absolute/workspace/aliwwforlinux/Bin/readme.txt-decrypt");

    assert(ret);

    BinFile testbin;
    testbin.Open("/Users/absolute/workspace/aliwwforlinux/Bin/readme.txt", O_RDONLY);
    assert(testbin.CopyTo("/Users/absolute/workspace/temptest/readme_copyto.txt"));
    printf("All Pass.\n");

    return 0;
}
